package scripts;
import bot.ScriptMethods;

public class GuildMiner extends ScriptMethods implements Runnable {

	public Thread scriptThread;
	public boolean scriptRunning = false;

	public void start() {
		getAccountSettings();
		System.gc();
		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
		scriptThread = null;
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();
				if (bankOpen()) {
					if (invContains(155)) { 
						depositAll(155);
						wait(600);
					}
					if (invContains(157)) { 
						depositAll(157);
						wait(random(600, 750));
					}
					if (invContains(158)) { 
						depositAll(158);
						wait(random(600, 750));
					}
					if (invContains(159)) { 
						depositAll(159);
						wait(random(600, 750));
					}
					if (invContains(160)) { 
						depositAll(160);
						wait(random(600, 750));
					}
					closeBank();
					wait(600);
				}

				if (ourPlayerY() > 3000) {
					if (getInventorySize() == 30) {
						atObject(274, 3398);
						wait(random(1000, 1500));
					} else {
						int[] coal = getObjectById(110);
						if (coal[0] != -1 && !isSleeping()) {
							handleEvents();
							atObject(coal[1], coal[2]);
							wait(random(600, 1000));
						}
					}
				} else {
					int[] bankDoor = getObjectById(64);
					if (bankDoor[0] != -1) {
						atObject(bankDoor[1], bankDoor[2]);
						wait(random(1000, 1200));
					} else {
						if (getInventorySize() > 4) {
							if (!bankOpen()) {
								openBank();
								wait(1000);
							}
						} else {
							atObject(274, 566);
							wait(random(1500, 2500));
						}
					}
				}
				wait(150);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 85 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
}